package cn.wolfcode.servlet;

import cn.wolfcode.domain.Student;

import cn.wolfcode.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@ContextConfiguration("classpath:Application_context_student.xml")
@WebServlet("/indexServlet")
public class indexServlet extends HttpServlet {
    //ApplicationContext ctx = new ClassPathXmlApplicationContext("Application_context_student.xml");
    //private IStudentService service= (IStudentService) ctx.getBean("studentService");
    @Autowired
    private IStudentService service;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String cmd = req.getParameter("cmd");
        System.out.println(cmd);
        if("delete".equals(cmd)){
            try {
            delete(req, resp);
            }catch (Exception e){
                System.out.println("错误");
            }
        }else  if("input".equals(cmd)){
            try {
            input(req, resp);
        }catch (Exception e){
                e.printStackTrace();
            System.out.println("错误");
        }
        }else if("saveOrUpdate".equals(cmd)){
            try {
                saveOrUpdate(req, resp);
            }catch (Exception e){
                System.out.println("错误");
            }
        }else if("input2".equals(cmd)){
            try {
                input2(req, resp);
            }catch (Exception e){
                System.out.println("错误");
            }

        }else if("saveOrUpdate2".equals(cmd)){
            try {
                saveOrUpdate2(req,resp);
            }catch (Exception e){
                System.out.println("错误");
            }

        }else {
            try {
                list(req,resp);
            }catch (Exception e){
                System.out.println("错误");
            }
        }
    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Student> list =  service.se();
        req.setAttribute("list",list);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
    protected void input(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        if (!id.equals(null)) {
            Long i = Long.parseLong(id);
            Student student =service.seid(i);
            System.out.println(student);
            req.setAttribute("student",student);
            req.getRequestDispatcher("/xiugai.jsp").forward(req, resp);
            return;
        }
    }
    protected void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Long id = Long.parseLong(req.getParameter("id"));
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        service.up(id,username,password);
        resp.sendRedirect("/indexServlet");
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String strId = req.getParameter("id");
        System.out.println(strId);
        service.del(Long.parseLong(strId));
        resp.sendRedirect("/indexServlet");
    }
    protected void input2(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.sendRedirect("/tianjia.jsp");
    }
    protected void saveOrUpdate2(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        service.save(username,password);
        resp.sendRedirect("/indexServlet");
    }
}
